// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2011 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;


namespace CharityGamesOnline.Modules.Game.Trivia
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The Settings class manages Module Settings 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    partial class Settings : ModuleSettingsBase
    {

        #region "Base Method Implementations"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// LoadSettings loads the settings from the Database and displays them 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public override void LoadSettings()
        {
            try
            {
                if (!IsPostBack)
                {
                    if (Directory.Exists(Request.MapPath(ControlPath)))
                    {                        
                        string[] arrFiles;
                        arrFiles = Directory.GetFiles(Request.MapPath(ControlPath), "*.asmx");

                        foreach (string strFile in arrFiles)
                        {
                            string file = Path.GetFileName(strFile);
                            ddlTriviaWebServiceUrl.Items.Add(new ListItem(file, file.ToLower()));
                        }

                        if (!Null.IsNull(ModuleSettings["TriviaWebServiceUrl"]) && !string.IsNullOrEmpty((string)ModuleSettings["TriviaWebServiceUrl"]))
                        {
                            ListItem selItem = ddlTriviaWebServiceUrl.Items.FindByValue(ModuleSettings["TriviaWebServiceUrl"].ToString());
                            if (selItem != null)
                                selItem.Selected = true;
                        }
                    }

                    if (Directory.Exists(Request.MapPath(ControlPath+"Flash")))
                    {
                        string[] arrFiles;
                        arrFiles = Directory.GetFiles(Request.MapPath(ControlPath + "Flash"), "*.swf");

                        foreach (string strFile in arrFiles)
                        {
                            string file = Path.GetFileName(strFile);
                            ddlTriviaFlash.Items.Add(new ListItem(file, file.ToLower()));
                        }

                        if (!Null.IsNull(ModuleSettings["TriviaFlashFile"]) && !string.IsNullOrEmpty((string)ModuleSettings["TriviaFlashFile"]))
                        {
                            ListItem selItem = ddlTriviaFlash.Items.FindByValue(ModuleSettings["TriviaFlashFile"].ToString());
                            if (selItem != null)
                                selItem.Selected = true;
                        }

                        arrFiles = Directory.GetFiles(Request.MapPath(ControlPath + "Flash"), "*.xml");

                        foreach (string strFile in arrFiles)
                        {
                            string file = Path.GetFileName(strFile);
                            ddlPracticeData.Items.Add(new ListItem(file, file.ToLower()));
                        }

                        if (!Null.IsNull(ModuleSettings["PracticeData"]) && !string.IsNullOrEmpty((string)ModuleSettings["PracticeData"]))
                        {
                            ListItem selItem = ddlPracticeData.Items.FindByValue(ModuleSettings["PracticeData"].ToString());
                            if (selItem != null)
                                selItem.Selected = true;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// UpdateSettings saves the modified settings to the Database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public override void UpdateSettings()
        {
            try
            {
                ModuleController objModules = new ModuleController();

                objModules.UpdateModuleSetting(ModuleId, "TriviaWebServiceUrl", ddlTriviaWebServiceUrl.SelectedValue);
                objModules.UpdateModuleSetting(ModuleId, "TriviaFlashFile", ddlTriviaFlash.SelectedValue);
                objModules.UpdateModuleSetting(ModuleId, "PracticeData", ddlPracticeData.SelectedValue);                
            }

            catch (Exception exc)
            {
                //Module failed to load 
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

    }

}

